package xyz.xuminghai.javafx_learn.animation.transition;

import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * 2023/7/22 12:58 星期六<br/>
 * 此Transition创建一个跨越其duration路径动画。
 * 沿路径的平移是通过更新node的translateX和translateY变量来完成的，
 * 如果orientation设置为OrientationType.ORTHOGONAL_TO_TANGENT ，
 * 则rotate变量将定期更新。
 *
 * @author xuMingHai
 */
public class PathTransitionApp extends Application {


    private final BorderPane root = new BorderPane();

    public static void main(String[] args) {
        Application.launch(args);
    }

    private Shape getPath() {
        Rectangle rectangle = new Rectangle(600.0, 400.0, Color.TRANSPARENT);
        rectangle.setStroke(Color.BLACK);
        return rectangle;
    }

    @Override
    public void init() {
        Rectangle rectangle = new Rectangle(50.0, 50.0, Color.RED);
        rectangle.setTranslateX(-25.0);
        rectangle.setTranslateY(-25.0);

        Shape path = getPath();
        path.setStroke(Color.BLUE);
        PathTransition pathTransition = new PathTransition(Duration.millis(5000.0),
                path, rectangle);
        // 1倍速反向播放
        pathTransition.setRate(-1.0);

        Button button = new Button("播放");
        button.setOnAction(event -> pathTransition.play());

        Pane pane = new Pane(path, rectangle);
        Rectangle rectangle1 = new Rectangle(650.0, 450.0, Color.TRANSPARENT);
        rectangle1.setStroke(Color.BLACK);

        StackPane.setMargin(pane, new Insets(100.0));
        StackPane.setAlignment(button, Pos.TOP_CENTER);
        StackPane stackPane = new StackPane(rectangle1, pane, button);
        stackPane.setBackground(new Background(new BackgroundFill(Color.BEIGE, null, null)));
        root.setCenter(stackPane);
    }


    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
